#!/usr/bin/env python3

import sys
import datetime
import os
import subprocess
import re  # 正则
import os.path

test_script_flag=1
work_space_root="/home/nio/repos"

test_ext_dir=""


#------------------------------------------------
if test_script_flag == 0:
  print("process file authorization cp in docker cosine")
  map_engine_sdk_target_dir=work_space_root+test_ext_dir+"/localization_external/opt/nio/aarch64/cosine"
  map_engine_sdk_target_inc_dir=map_engine_sdk_target_dir+"/include/map_module"
  map_engine_sdk_target_lib_dir=map_engine_sdk_target_dir+"/lib"
  os.system('sudo chown -R $(whoami) %s'%(map_engine_sdk_target_inc_dir))
  os.system('sudo chown -R $(whoami) %s/libmap_module.so %s/libmap_proto.so'%(map_engine_sdk_target_lib_dir,map_engine_sdk_target_lib_dir))
#------------------------------------------------


#-------------------x86-----------------------------
if test_script_flag == 0:
  print("process x86")
  map_engine_sdk_source_dir="/home/nio/repos/map_engine_lib_repo/map_engine_sdk"
  map_engine_sdk_source_inc_dir=map_engine_sdk_source_dir+"/include"
  map_engine_sdk_source_lib_dir=map_engine_sdk_source_dir+"/lib"

  map_engine_sdk_target_dir=work_space_root+test_ext_dir+"/localization_external/opt/nio/x86_64"
  map_engine_sdk_target_inc_dir=map_engine_sdk_target_dir+"/include/map_module"
  map_engine_sdk_target_lib_dir=map_engine_sdk_target_dir+"/lib"
  os.system('rm -rf %s/*'%(map_engine_sdk_target_inc_dir))
  os.system('cp -rf %s/* %s'%(map_engine_sdk_source_inc_dir,map_engine_sdk_target_inc_dir))
  os.system('rm -rf %s/libmapengine.so'%(map_engine_sdk_target_lib_dir))
  os.system('cp -rf %s/libmapengine.so %s'%(map_engine_sdk_source_lib_dir,map_engine_sdk_target_lib_dir))
#-------------------x86-----------------------------




#-------------------arm_cyber-----------------------------
if test_script_flag == 0:

  print("process arm cyber")
  map_engine_sdk_target_dir=work_space_root+test_ext_dir+"/localization_external/opt/nio/aarch64"
  map_engine_sdk_target_inc_dir=map_engine_sdk_target_dir+"/include/map_module"
  map_engine_sdk_target_lib_dir=map_engine_sdk_target_dir+"/lib"

  map_engine_sdk_source_dir=work_space_root+"/map_engine_src/map_engine_for_arm_lib"
  map_engine_sdk_source_inc_dir=map_engine_sdk_target_dir+"/cosine"+"/include/map_module"
  map_engine_sdk_source_lib_dir=map_engine_sdk_source_dir+"/build"

  os.system('cd %s && \
            rm -rf build && \
            mkdir -p build && \
            cd build'%(map_engine_sdk_source_dir))

  #  如果报错，sudo dpkg-reconfigure dash 然后选择no，然后就不报错了
  os.system('\
            cd ~/pnc_cyber_release && git pull && \
            cd %s/build && \
            source ~/pnc_cyber_release/env_setup.sh aarch64 && \
            cmake .. -DCMAKE_DIR=/home/nio/repos/localization/newest_feature_1/localization/src/cyber_ws/cmake             -DINSTALL_DIR=/home/nio/repos/localization/newest_feature_1/localization/src/cyber_ws/install             -DCMAKE_TOOLCHAIN_FILE=/home/nio/pnc_cyber_release/../mazu_dev/aarch64/cmake/toolchain_linux.cmake             -DARCH=aarch64             -DCMAKE_SYSTEM_PROCESSOR=aarch64             -DCMAKE_BUILD_TYPE=Release             -DBUILDING_ALL_GTESTS=OFF && \
            make -j12 \
            '%(map_engine_sdk_source_dir))

  os.system('\
          rm -rf %s/*\
            '%(map_engine_sdk_target_inc_dir))

  os.system('\
            cp -rf %s/* %s\
              '%(\
                map_engine_sdk_source_inc_dir,map_engine_sdk_target_inc_dir\
                  ))

  os.system('\
            rm -rf %s/libmap_module.a %s/libmap_proto.a\
              '%(\
                map_engine_sdk_target_lib_dir,map_engine_sdk_target_lib_dir
                ))

  os.system('\
            cp -rf %s/libmap_module.a %s/interface/proto/libmap_proto.a %s\
              '%(\
                map_engine_sdk_source_lib_dir,map_engine_sdk_source_lib_dir,map_engine_sdk_target_lib_dir\
                  ))
#-------------------arm_cyber-----------------------------



#------------------------------------------------

print("localization_external repo")
localization_ext_repo_dir           =work_space_root+"/localization_external"

localization_external_repo_tag_new=sys.argv[1]
print(localization_external_repo_tag_new)

""" 在 os.system当中 commit后面 跟的 其他命令 无法执行 很奇怪 另开一个system即可 """

print("[cmd_list]:\n\
cd "+localization_ext_repo_dir+" && \\\n\
git add . && git commit -am \"update map engine sdk\" && \\\n\
git tag -a "+localization_external_repo_tag_new+" -m \"update map engine sdk\""
)
print("\
cd "+localization_ext_repo_dir+" && \
git add . && git commit -am \"update map engine sdk\" \
git tag -a "+localization_external_repo_tag_new+" -m \"update map engine sdk\""
)

print("[cmd_list]:\n\
git push origin -u -f feature/update_map_engine_sdk && \\\n\
git push origin --tags"
)
print("\
git push origin -u -f feature/update_map_engine_sdk && \
git push origin --tags"
)
#------------------------------------------------





